package com.xrr.shardingdemo.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrr.shardingdemo.mapper.OrderMapper;
import com.xrr.shardingdemo.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import com.xrr.shardingdemo.entity.*;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/insertOrder")
    public Integer insertOrder(){
        int randomInt = ThreadLocalRandom.current().nextInt(100);
        return orderMapper.insert(new Order(null,String.valueOf(randomInt%2+1),randomInt ));
    }

    @GetMapping("/getOrderById")
    public Order getOrderById(Long orderId){
        return orderMapper.selectById(orderId);
    }


    @GetMapping("/getAllOrder")
    public Object getAllOrder(int size){
       return orderMapper.selectList(new QueryWrapper<>());
    }

    @GetMapping("/getPageOrder")
    public Object getPageOrder(int size){
        IPage<Order> page = new Page<>(1,size);
        return orderMapper.selectPage(page,new QueryWrapper<Order>().orderBy(true,true,"user_id"));

    }

    @GetMapping("/insertUser")
    public Integer insertUser(){
       User user =  new User(System.currentTimeMillis(),System.currentTimeMillis());
       return userMapper.insert(user);
    }
}
